/*  Initially created to learn Java syntax.
	Finalised by OswaldTechnic, with help from ArcanePhysics and ChatGPT.
*/
public class Main {
	final static String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
	final static String UPPERCASE = LOWERCASE.toUpperCase();
	public static void main(String[] args) {
		String inputTxt = ("This is the test message.");
		System.out.println("Here is the original:\n" + inputTxt + "\n");
		System.out.println("Caesar'ed': " + caesar(1,(inputTxt)));
		System.out.println("Vigenere'ed': " + vigenere("i'm shifty",(inputTxt)));
//		System.out.println(shiftChar(2, "a"));
	}
	static String shiftChar(int shift, String character) {
		//TODO rearrange this math. The robot's math also will not work.
		while (shift<0) shift += 26;		//My own math that allows for very low values.
		shift = shift % 26 % 26;	 //chatGPT math that allows for very high values.
		if (LOWERCASE.contains(character)) {
			//checks if the letter wraps around the alphabet, then removes 26 if true.
			if ((LOWERCASE.indexOf(character) + shift) < 26) {
				character = String.valueOf(
						LOWERCASE.charAt(
								LOWERCASE.indexOf(character) + shift));
			} else character = String.valueOf(
					LOWERCASE.charAt(
							(LOWERCASE.indexOf(character) + shift) - 26));
		} else if ((UPPERCASE.contains(character))) {
			//checks if the letter wraps around the alphabet, then removes 26 if true.
			if ((UPPERCASE.indexOf(character) + shift) < 26) {
				character = String.valueOf(
						UPPERCASE.charAt(
								UPPERCASE.indexOf(character) + shift));
			}
		}
		return character;
	}
	static String caesar(int shift, String txt) {
		String shifting_character = "";
		StringBuilder output_txt = new StringBuilder();
		for (int i = 0; i < txt.length(); i++) {
			shifting_character = String.valueOf(txt.charAt(i));
			output_txt.append(shiftChar(shift, shifting_character));	   // finally adds the character to the output.
		}
		return String.valueOf(output_txt);
	}
	static String vigenere(String keyPhrase, String txt) {
		String shifting_character = "";
		StringBuilder output_txt = new StringBuilder();
		int shift = 0;
		String a;
		for (int i = 0; i < txt.length(); i++) {
			//the current character in the key.
			a = String.valueOf(keyPhrase.charAt(i % keyPhrase.length()));
			//the index of "a" in the alphabet.
			if (LOWERCASE.contains(a)) {
				shift = LOWERCASE.indexOf(a) + 1;
			} else if (UPPERCASE.contains(a)) {
				shift = UPPERCASE.indexOf(a) + 1;
			}
			shifting_character = String.valueOf(txt.charAt(i));
			output_txt.append(shiftChar(shift, shifting_character));       // finally adds the character to the output.
		}
		return String.valueOf(output_txt);
	}
}   //end class